package org.itzheng.myrxframe.realize.apk.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.Uri;

/**
 * Title:<br>
 * Description: <br>
 *
 * @email ItZheng@ZoHo.com
 * Created by itzheng on 2019-4-16.
 */
public class AppUtils {
    public static Drawable getAppIcon(PackageManager packageManager, PackageInfo packageInfo) {
        return packageInfo.applicationInfo.loadIcon(packageManager);
    }

    public static String getAppName(PackageManager packageManager, PackageInfo packageInfo) {
        return packageInfo.applicationInfo.loadLabel(packageManager)
                .toString();
    }

    public static String getAppPackageName(PackageInfo packageInfo) {
        return packageInfo.packageName;
    }

    public static String getAppVersionName(PackageInfo packageInfo) {
        return packageInfo.versionName;
    }

    /**
     * 这个是apk包 xxx.apk
     *
     * @param packageInfo
     * @return
     */
    public static String getAppSourceDir(PackageInfo packageInfo) {
        return packageInfo.applicationInfo.sourceDir;
    }

    /**
     * 获取包信息
     *
     * @param context
     * @param packageName
     * @return
     */
    public static PackageInfo getPackageInfo(Context context, String packageName) {
        //包管理器对象
        PackageManager pm = context.getPackageManager();
        //检查包名
        PackageInfo packageInfo = null;
        try {
            packageInfo = pm.getPackageInfo(packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return packageInfo;
    }

    public static void deleteApp(Context context, String packageName) {
        Uri uri = Uri.fromParts("package", packageName, null);
        Intent intent = new Intent(Intent.ACTION_DELETE, uri);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /*
        *启动设备已安装的app
        */
    public static void openApp(Context context, String packageName) {
        openApp(context, packageName, null);
    }

    /*
    *启动设备已安装的app
    */
    public static void openApp(Context context, String packageName, IOpenAppResult result) {
        //包管理器对象
        PackageManager pm = context.getPackageManager();
        //检查包名
        PackageInfo packageInfo = getPackageInfo(context, packageName);

        //包名存在则启动app
        if (packageInfo != null) {
            Intent intent = pm.getLaunchIntentForPackage(packageName);
            if (intent == null) {
                if (result != null) {
                    result.OnError("Launch Intent is null");
                }
                return;
            }
            context.startActivity(intent);
        } else {
            if (result != null) {
                result.OnError(packageName + " Not Found ");
            }
        }
    }


    public interface IOpenAppResult {
        void OnSuccess();

        void OnError(String error);
    }
}
